package com.jizi.machine.service;

import com.tencentcloudapi.common.AbstractModel;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.ocr.v20181119.OcrClient;
import com.tencentcloudapi.ocr.v20181119.models.*;

/**
 * 腾讯云OCR服务
 * @author scy
 * @date 2025/2/13 15:30
 */
public class TencentOCRService {
 
    private static final String REGION = "ap-guangzhou"; // 区域
 
    private OcrClient client;
 
    public TencentOCRService(String secretId, String secretKey) {
        // 初始化客户端
        Credential cred = new Credential(secretId, secretKey);
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint("ocr.tencentcloudapi.com"); // 接口地址
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        client = new OcrClient(cred, REGION, clientProfile);
    }
 
    public String recognizeIDCardByImageBase64(String imageBase64, String imageUrl, String cardSide) throws TencentCloudSDKException {
        // 构造请求参数
        IDCardOCRRequest req = new IDCardOCRRequest();
        req.setImageBase64(imageBase64);
        req.setImageUrl(imageUrl);
        req.setCardSide(cardSide); // FRONT（正面）或 BACK（反面）
 
        // 调用接口
        IDCardOCRResponse resp = client.IDCardOCR(req);
 
        // 返回识别结果
        return AbstractModel.toJsonString(resp);
    }
 
    public String recognizeIDCardByUrl(String imageUrl, String cardSide) throws TencentCloudSDKException {
        // 构造请求参数
        IDCardOCRRequest req = new IDCardOCRRequest();
        req.setImageUrl(imageUrl);
        req.setCardSide(cardSide); // FRONT（正面）或 BACK（反面）
 
        // 调用接口
        IDCardOCRResponse resp = client.IDCardOCR(req);
 
        // 返回识别结果
        return AbstractModel.toJsonString(resp);
    }

    /**
     * 银行卡识别接口
     *
     * @param imageBase64
     * @param imageUrl
     * @return
     */
    public String recognizeBankCardByImageBase64(String imageBase64, String imageUrl) throws TencentCloudSDKException {
        // 构造请求参数
        BankCardOCRRequest req = new BankCardOCRRequest();
        req.setImageBase64(imageBase64);
        req.setImageUrl(imageUrl);
        // 调用接口
        BankCardOCRResponse resp = client.BankCardOCR(req);

        // 返回识别结果
        return AbstractModel.toJsonString(resp);
    }

    /**
     * 企业营业执照
     * @param imageBase64
     * @param imageUrl
     * @return
     */
    public String recognizeBusinessLicenseByImageBase64(String imageBase64, String imageUrl) throws TencentCloudSDKException {
        // 构造请求参数
        BizLicenseOCRRequest req = new BizLicenseOCRRequest();
        req.setImageBase64(imageBase64);
        req.setImageUrl(imageUrl);
        // 调用接口
        BizLicenseOCRResponse resp = client.BizLicenseOCR(req);

        // 返回识别结果
        return AbstractModel.toJsonString(resp);
    }
 
    public static void main(String[] args) {
        TencentOCRService ocrService = new TencentOCRService("secretId", "secretKey");
        try {
            String result = ocrService.recognizeIDCardByUrl("https://ocr-demo-1254418846.cos.ap-guangzhou.myqcloud.com/card/IDCardOCR/IDCardOCR1.jpg", "FRONT");
            System.out.println(result);
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
        }
    }
}